package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceMpwService;
import com.eastjet.maintenance.domain.DeviceLevel;
import com.eastjet.maintenance.domain.DeviceMel;
import com.eastjet.maintenance.domain.DeviceMpw;
import com.eastjet.maintenance.domain.DeviceRkb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mpw")
public class MpwController {

    @Autowired
    private DeviceMpwService deviceMPWService;

    @RequestMapping("/list")
    public Page<DeviceMpw>  list(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            // 返回空的 Page<DeviceFt> 对象
            return  deviceMPWService.getDeviceMpwList(0, 0, "");
        }

        Integer page = (Integer) map.get("page");
        Integer size = (Integer) map.get("size");
        String condition = (String) map.get("condition");

        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        System.out.println("size:" + size);
        return deviceMPWService.getDeviceMpwList(page, size, condition);
    }

    @RequestMapping("/add")
    public String add( @RequestBody Map<String, Object> map) {
        String TableName;
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.get("TableName") == null || map.isEmpty()){
            //返回错误信息
            return "TableName is null or empty";
        }else
        {
            TableName = (String) map.get("TableName");
           DeviceMpw deviceMpw = new DeviceMpw();
            deviceMpw.setCode((String) map.get("code"));
            // 使用 Integer.valueOf 转换整数
            deviceMpw.setMpwName((String) map.get("name"));
            deviceMpw.setMpwLevelId((String) map.get("mpwLevelId"));
            deviceMpw.setMpwPart((String) map.get("melPartId"));
            deviceMpw.setMpwState(Integer.valueOf(map.get("state").toString()));
            deviceMpw.setMpwRequire((String) map.get("mpwRequire"));
            deviceMpw.setNotas((String) map.get("notes"));
            return deviceMPWService.addDeviceMpw(deviceMpw,TableName);
        }
    }

    @RequestMapping("/update")
    public String update( @RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceMpw deviceMpw = new DeviceMpw();
        deviceMpw.setCode((String) map.get("code"));
        // 使用 Integer.valueOf 转换整数
        deviceMpw.setMpwName((String) map.get("name"));
        deviceMpw.setMpwLevelId((String) map.get("mpwLevelId"));
        deviceMpw.setMpwPart((String) map.get("melPartId"));
        deviceMpw.setMpwState(Integer.valueOf(map.get("state").toString()));
        deviceMpw.setMpwRequire((String) map.get("mpwRequire"));
        deviceMpw.setNotas((String) map.get("notes"));
        deviceMpw.setMpwId(Integer.valueOf(map.get("id").toString()));
        return deviceMPWService.updateDeviceMpw(deviceMpw);

    }

    @RequestMapping("/delete")
    public String delete( @RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态
        DeviceMpw deviceMpw = new DeviceMpw();
        deviceMpw.setIsDelete(1);



        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceMpw.setMpwId(id);
                deviceMPWService.deleteDeviceMpw(deviceMpw);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }

    @RequestMapping("/updateState")
    public String updateState( @RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 state 值
        Integer state = (Integer) map.get("state");
        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态

        DeviceMpw deviceMpw = new DeviceMpw();
        deviceMpw.setMpwState(state);

        // 打印获取到的 state 值
        System.out.println("state: " + state);

        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceMpw.setMpwId(id);
                deviceMPWService.updateDeviceMpwState(deviceMpw);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }

    @RequestMapping("/getDeviceLevel")
    public List<DeviceLevel> getDeviceLevels()
    {
      return   deviceMPWService.getDeviceLevelList();
    }
}
